import Toast from 'tdesign-miniprogram/toast/index';
import homeApi from '../../services/home/home';
import teacherTypeApi from '../../services/teacherType/teacherType';
import {
  login
} from '../../services/user/user.js';

import WebSocketService from '../../services/websocket/websocket.js';

Page({
  data: {
    hotWord: '软件开发',
    wsService: null,
    queryParam: {
      key: null,
      type: null,
      page: 0,
      size: 10
    },
    navIndex: 0,
    recommendTypes: [],
    recommendNames: [],
    groupTeachers: [],
    teacherTypes: [],
    imgSrcs: [
      'https://my-teachers.obs.cn-north-4.myhuaweicloud.com/img/%E8%BD%AE%E6%92%AD2.png',
      'https://my-teachers.obs.cn-north-4.myhuaweicloud.com/img/%E8%BD%AE%E6%92%AD1.png',
      'https://my-teachers.obs.cn-north-4.myhuaweicloud.com/img/%E8%BD%AE%E6%92%AD3.png'
    ],
    goodsList: [],
    goodsListLoadStatus: 0,
    pageLoading: false,
    current: 1,
    autoplay: true,
    duration: '500',
    interval: 5000,
    navigation: {
      type: 'dots'
    },
    swiperImageProps: {
      mode: 'scaleToFill'
    },
    recommendTeachers: []
  },

  goodListPagination: {
    index: 0,
    num: 20,
  },

  privateData: {
    tabIndex: 0,
  },

  onShow() {
    this.getTabBar().init();
  },

  onLoad() {
    this.login().then(res => {
      console.log("getApp().globalData.userInfo", getApp().globalData.userInfo)
      this.init();
      // 连接websocket
      const wsService = new WebSocketService();
      wsService.connect(getApp().globalData.userInfo.userId);
      getApp().globalData.wsService = wsService
    })
  },

  onReachBottom() {},

  onPullDownRefresh() {
    this.init();
  },

  init() {
    this.loadHomePage();
  },

  login: function () {
    return new Promise((resolve, reject) => {
      console.log("login")
      wx.login({
        success: (result) => {
          console.log(result)
          if (result.code) {
            login(result.code).then((res) => {
              console.log(res)
              getApp().globalData.userInfo = res;
              resolve();
            });
          }
        }
      })
    });
  },

  loadHomePage() {
    wx.stopPullDownRefresh();
    this.setData({
      pageLoading: true,
    });
    homeApi.findRecommend(this.data.queryParam).then(res => {
      this.setData({
        recommendTeachers: res,
        pageLoading: false
      })
    });
    teacherTypeApi.getTeacherTypeList().then(res => {
      this.setData({
        teacherTypes: res
      })
    })
  },

  navToSearchPage() {
    wx.navigateTo({
      url: '/pages/goods/search/index'
    });
  },

  navChange: function (index) {
    console.log(index)
    this.setData({
      navIndex: index.detail
    });
  },

  toDetail(e) {
    wx.navigateTo({
      url: `/pages/teacherDetail/teacherDetail?id=${e.currentTarget.dataset.id}`,
    });
  },

  toTypeList(e) {
    wx.navigateTo({
      url: `/pages/teacherList/index?typeCode=${e.currentTarget.dataset.code}&typeText=${e.currentTarget.dataset.text}`,
    });
  }
});